// pages/addAddress/addAddress.js
var api = require('../../utils/http.js');
const app = getApp();
const { $Toast } = require('../../dist/base/index');
Page({

	/**
	 * 页面的初始数据
	 */
  data: {
    sexId: 0, //默认男
    schoolArr: [],
    index: '', //学校选择默认下标
    schoolArr1: [],
    index1: '', //宿舍选择默认下标
    isclick: false,
  },


  //性别选择
  chooseSex(e) {
    let sexId = e.currentTarget.dataset.id;
    this.setData({
      sexId
    })
  },

  //学校选择
  bindSchool: function (e) {
    // var schoolArr = this.data.schoolArr;
    // var nowArr = schoolArr[e.detail.value];

    // this.setData({
    // 	index: e.detail.value,
    //   nowArr
    // })
    wx.navigateTo({
      url: '../chooseSchool/chooseSchool1',
    })


  },

  //宿舍选择
  bindSchool1: function (e) {
    var nowSushe = this.data.schoolArr1[this.data.index1]
    this.setData({
      index1: e.detail.value,
      nowSushe
    })
  },

  //查询所有学校
  getAllSchool() {
    api.fetchApi('procedure_t', { p: 'R2005003' }).then(res => {
      if (res.data.code == 1) {
        var allSchool = res.data.results;
        this.setData({
          schoolArr: allSchool
        })

        this.data.opt_type == 0 ? this.editAddress() : "";

      }
    })
  },
  //表单提交
  formSubmit(e) {
   
    if (!this.data.isclick) {
      // let telReg = /^1((3[\d])|(4[5,6,9])|(5[0-3,5-9])|(6[5-7])|(7[0-8])|(8[1-3,5-8])|(9[1,8,9]))\d{8}$/;
      let telReg = /^1[3456789]\d{9}$/;
      let addressObj = e.detail.value
      console.log(addressObj)
      if (!addressObj.name.trim()) {
        $Toast({
          content: '请输入姓名',
          type: 'warning'
        });
        return
      }
      if (!(telReg.test(addressObj.tel))) {
        $Toast({
          content: '手机号格式不正确',
          type: 'warning'
        });
        return
      }
      if (!addressObj.school) {
        $Toast({
          content: '请选择所在学校',
          type: 'warning'
        });

        return
      }
      if (!addressObj.sushe) {
        $Toast({
          content: '请选择宿舍楼',
          type: 'warning'
        });
        return
      }
      if (!addressObj.roomNum) {
        $Toast({
          content: '请填写宿舍号',
          type: 'warning'
        });   
        return
      }

      var nowSchool_id = this.data.schoolArr[this.data.index].id;
      if (this.data.schoolArr1.length==0){
        var nowSushe_id=0
      }else{
        var nowSushe_id = this.data.schoolArr1[this.data.index1].id;
      }
      
    
      console.log(nowSushe_id)
      var uid = app.globalData.uid;
      console.log(e.detail.value)

      var params = {
        p: `R2005002|${uid}|${addressObj.name}|${addressObj.tel}|${nowSushe_id}|${addressObj.roomNum}|0|${nowSchool_id}|${this.data.sexId}`
      }
      var params1 = {
        p: `R2005001|${this.data.address_id}|${uid}|${addressObj.name}|${addressObj.tel}|${nowSushe_id}|${addressObj.roomNum}|0|${nowSchool_id}|${this.data.sexId}`
      }

      if (this.data.opt_type == 0) {
        //编辑地址
        api.fetchApi('procedure_t', params1).then(res => {
          if (res.data.code == 1) {
            $Toast({
              content: '编辑成功',
              type: 'success'
            });
            setTimeout(() => {
              wx.navigateBack({

              })
            }, 1000)
           
          }
        })
      } else {
        //添加地址
        api.fetchApi('procedure_t', params).then(res => {
          if (res.data.code == 1) {
            $Toast({
              content: '添加成功',
              type: 'success'
            });
            setTimeout(() => {
              wx.navigateBack({

              })
            }, 500)
      
          }
        })
      }
    }

    //防止多次点击
    this.setData({
      isclick: true
    })

  },

  //上个页面修改地址带过来的数据
  editAddress() {
    api.fetchApi('procedure_t', {
      p: 'R2003010|' + this.data.address_id
    }).then(res => {

      if (res.data.code == 1) {
        var addressItem = res.data.results[0];
        if (addressItem.gender == 0) {
          this.setData({
            sexId: 0
          })
        } else {
          this.setData({
            sexId: 1
          })
        }
        this.setData({
          addressItem
        })
      }
      //拿学校id查楼名
      api.fetchApi('procedure_t', { p: 'R2012003|' + addressItem.school_id }).then(res => {
        if (res.data.code == 1) {
          var schoolArr1 = res.data.results;
          this.setData({
            schoolArr1
          })
          var schoolArr1 = this.data.schoolArr1;
          var index1 = schoolArr1.findIndex(item => item.id === addressItem.sushelou);
          this.setData({
            index1
          })
        }
      })

      var schoolArr = this.data.schoolArr;
      var index = schoolArr.findIndex(item => item.school_name === addressItem.school);
      
      this.setData({
        index
       
      })
    })


  },
	/**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
    console.log(options)
    var address_id = options.address_id;
    var opt_type = options.type;
    this.setData({
      address_id,
      opt_type // 0编辑 1添加
    })
    this.getAllSchool();

   

  },

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function () {

  },

	/**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function () {
   
      var schoolArr = this.data.schoolArr;
    if (wx.getStorageSync('isSelect')){
      var nowArr = wx.getStorageSync('nowSchool1');
      var index = schoolArr.findIndex(item => item.id == nowArr.id)
      api.fetchApi('procedure_t', {p: 'R2012003|' + nowArr.id}).then(res=>{
        if(res.data.code==1){
          var schoolArr1 = res.data.results;
          console.log(schoolArr1)
          this.setData({
            schoolArr1
          })
        }
      })
      this.setData({
        nowArr,
        index
      })
    }
      
    
   
  },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function () {

  },

	/**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function () {
    wx.removeStorageSync('isSelect')
  },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function () {

  },

	/**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function () {

  },

	/**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function () {

  }
})
